import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher_string.dart';

import 'src/theme.dart';
export 'src/theme.dart';
export 'src/icons.dart';
export 'src/bottom_navigation_bar.dart';
export 'src/tap.dart';
export 'src/list_cell.dart';
export 'src/avatar.dart';
export 'src/panel.dart';
export 'src/round_underline_tab_indicator.dart';
export 'src/tag.dart';
export 'src/popup_menu.dart';
export 'src/badge.dart';
export 'src/backdrop_scaffold.dart';
export 'src/refresher.dart';
export 'src/menu_item.dart';
export 'src/remove_overscroll_indicator.dart';
export 'src/fold_up_text.dart';
export 'src/button.dart';
export 'src/loading_dialog.dart';
import 'src/loading_dialog.dart';
export 'src/verification_code.dart';
export 'src/actionsheet.dart';
export 'src/input.dart';
export 'src/lifecycle.dart';
export 'src/page_entrance.dart';
export 'src/state.dart';
export 'src/result.dart';
export 'src/search_bar.dart';
export 'src/open.dart';
export 'src/uploader.dart';
export 'src/image_preview.dart';
export 'src/policy_popup.dart';
export 'src/loading.dart';
export 'src/delay.dart';
export 'src/appbar.dart';
export 'src/video.dart';
export 'src/line_break.dart';
export 'src/linear_gradient_mask.dart';
export 'src/radial_gradient_mask.dart';
export 'src/block.dart';
export 'src/image.dart';
export 'src/right_menu_button_group.dart';

enum UiGravity {
  top,
  bottom,
  center,
  topLeft,
  topRight,
  bottomLeft,
  bottomRight,
  centerLeft,
  centerRight,
  snackbar,
  none
}

class Ui {
  static toast(
    String? text, {
    UiGravity gravity = UiGravity.center,
    Function()? onEnd,
  }) async {
    Map<UiGravity, ToastGravity> gravitys = {
      UiGravity.top: ToastGravity.TOP,
      UiGravity.bottom: ToastGravity.BOTTOM,
      UiGravity.center: ToastGravity.CENTER,
      UiGravity.topLeft: ToastGravity.TOP_LEFT,
      UiGravity.topRight: ToastGravity.TOP_RIGHT,
      UiGravity.bottomLeft: ToastGravity.BOTTOM_LEFT,
      UiGravity.bottomRight: ToastGravity.BOTTOM_RIGHT,
      UiGravity.centerLeft: ToastGravity.CENTER_LEFT,
      UiGravity.centerRight: ToastGravity.CENTER_RIGHT,
      UiGravity.snackbar: ToastGravity.SNACKBAR,
      UiGravity.none: ToastGravity.NONE
    };
    Fluttertoast.showToast(
      msg: ' $text ',
      toastLength: Toast.LENGTH_SHORT,
      gravity: gravitys[gravity] ?? ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.black87,
      textColor: Colors.white,
      fontSize: 14.0,
    );
    await 1.5.delay();
    onEnd?.call();
  }

  static Future alert(
    String? content, {
    Widget? header,
    Function()? onConfirm,
  }) async {
    return await showDialog(
      context: Get.context!,
      builder: (context) => Center(
        child: Container(
          constraints: const BoxConstraints(maxWidth: 400),
          padding: const EdgeInsets.all(30.0),
          child: Material(
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                header ?? Container(),
                Container(
                  padding: const EdgeInsets.all(20),
                  child: Text(
                    content ?? '',
                    style: const TextStyle(
                      fontSize: 18,
                    ),
                  ),
                ),
                const Divider(height: 0),
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                  width: double.infinity,
                  child: TextButton(
                    onPressed: () {
                      if (Navigator.of(context).canPop()) {
                        Navigator.of(context).pop();
                      }
                      onConfirm?.call();
                    },
                    child: const Text("确定"),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
      // builder: (context) => AlertDialog(
      //   title: const Text("提示"),
      //   content: Text(content ?? ''),
      //   actions: [TextButton(onPressed: () {}, child: Text("确定"))],
      // ),
    );
  }

  static showBottomSheet(child) async {
    return await showModalBottomSheet(
      context: Get.context!,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(UiTheme.boxRadiusX),
        ),
      ),
      builder: (context) => child,
    );
  }

  static showLoading({
    String loadingText = '加载中',
    bool showLoadingText = true,
  }) {
    LoadingDialog.show(
        loadingText: loadingText, showLoadingText: showLoadingText);
  }

  static hideLoading() {
    LoadingDialog.hidden();
  }

  static bool isNotEmpty(dynamic value) {
    return !isEmpty(value);
  }

  static bool isEmpty(dynamic value) {
    return value == null || value == '' || value == false || value.isEmpty;
  }

  /// 字符串为空时返回默认值
  static String getString(String? text, String defaultText) {
    if (text == '' || text == null) {
      return defaultText;
    } else {
      return text;
    }
  }

  ///复制内容
  static copyText(String? text, [Function(dynamic)? onCopied]) {
    if (text == '' || text == null) {
      return false;
    }
    Clipboard.setData(
      ClipboardData(text: text.toString()),
    );
    if (onCopied != null) {
      onCopied.call(text);
    } else {
      Ui.toast('已复制');
    }
  }

  static Future<bool> makePhoneCall(phone) async {
    if (phone == '' || phone == null) return false;
    return await launchUrlString('tel:$phone');
  }

  /// 格式化数字显示
  static String formatNumber(int number, [bool forTenThousand = false]) {
    if (forTenThousand) {
      if (number >= 10000) {
        return '${number.toStringAsFixed(1)}w';
      } else {
        return number.toString();
      }
    }
    String unit = '';
    num value = 0;
    if (number >= 10000 && number < 10000000) {
      unit = '万';
      value = number / 10000;
      // } else if (number >= 1000000 && number < 10000000) {
      //   unit = '百万';
      //   value = number / 1000000;
      // } else if (number >= 10000000 && number < 100000000) {
      //   unit = '千万';
      //   value = number / 10000000;
    } else if (number >= 100000000 && number < 1000000000000) {
      unit = '亿';
      value = number / 100000000;
    } else if (number >= 1000000000000) {
      unit = '兆';
      value = number / 1000000000000;
    } else {
      return number.toString();
    }
    return '${value.toStringAsFixed(1)}$unit';
  }

  static MaterialStateProperty<Color> createTextBtnBgColor() {
    return MaterialStateProperty.resolveWith((states) {
      if (states.contains(MaterialState.pressed)) {
        return Colors.transparent;
      } else if (states.contains(MaterialState.disabled)) {
        return Colors.transparent;
      }
      return Colors.transparent;
    });
  }

  /// 页面动画过渡完成
  static onPageAnimateCompleted(context, [Function()? callback]) {
    var route = ModalRoute.of(context);
    if (route != null) {
      route.animation?.addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          callback?.call();
        }
      });
    } else {
      callback?.call();
    }
  }
}
